package com.hycreate.platform.modules.unitpoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 积分Controller
 * @author wang
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/unitpoint/yhcgUnit")
public class YhcgUnitController extends BaseController {

	@Autowired
	private YhcgUnitService yhcgUnitService;
	
	@ModelAttribute
	public YhcgUnit get(@RequestParam(required=false) String id) {
		YhcgUnit entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yhcgUnitService.get(id);
		}
		if (entity == null){
			entity = new YhcgUnit();
		}
		return entity;
	}
	
	@RequiresPermissions("unitpoint:yhcgUnit:view")
	@RequestMapping(value = {"list", ""})
	public String list(YhcgUnit yhcgUnit, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<YhcgUnit> page = yhcgUnitService.findPage(new Page<YhcgUnit>(request, response), yhcgUnit); 
		model.addAttribute("page", page);
		return "modules/unitpoint/yhcgUnitList";
	}

	@RequiresPermissions("unitpoint:yhcgUnit:view")
	@RequestMapping(value = "form")
	public String form(YhcgUnit yhcgUnit, Model model) {
		model.addAttribute("yhcgUnit", yhcgUnit);
		return "modules/unitpoint/yhcgUnitForm";
	}

	@RequiresPermissions("unitpoint:yhcgUnit:edit")
	@RequestMapping(value = "save")
	public String save(YhcgUnit yhcgUnit, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, yhcgUnit)){
			return form(yhcgUnit, model);
		}
		yhcgUnitService.save(yhcgUnit);
		addMessage(redirectAttributes, "保存积分成功");
		return "redirect:"+Global.getAdminPath()+"/unitpoint/yhcgUnit/?repage";
	}
	
	@RequiresPermissions("unitpoint:yhcgUnit:edit")
	@RequestMapping(value = "delete")
	public String delete(YhcgUnit yhcgUnit, RedirectAttributes redirectAttributes) {
		yhcgUnitService.delete(yhcgUnit);
		addMessage(redirectAttributes, "删除积分成功");
		return "redirect:"+Global.getAdminPath()+"/unitpoint/yhcgUnit/?repage";
	}

}